Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.1k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+17.7k Golang : Simple client server example
+8.9k Golang : Intercept and compare HTTP response code example
+14k Golang : Check if a file exist or not
+5.9k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+18.3k Golang : Logging with logrus
+10.3k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+7.6k Golang : Test if an input is an Armstrong number example
+11.1k Golang : Intercept and process UNIX signals example
+79k Golang : How to return HTTP status code?
+25.1k Golang : Convert uint value to string type